package co.yixiang.modules.system.rest;

import lombok.AllArgsConstructor;
import cn.hutool.core.convert.Convert;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.system.domain.TyDeployInfo;
import co.yixiang.modules.system.service.TyDeployInfoService;
import co.yixiang.modules.system.dto.queryCriteria.TyDeployInfoQueryCriteria;
import co.yixiang.modules.system.dto.TyDeployInfoDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* @author DB
* @date 2020-08-22
*/
@AllArgsConstructor
@Api(tags = "团油配置管理")
@RestController
@RequestMapping("/api/tyDeployInfo")
public class TyDeployInfoController {

    private final TyDeployInfoService tyDeployInfoService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','tyDeployInfo:list')")
    public void download(HttpServletResponse response, TyDeployInfoQueryCriteria criteria) throws IOException {
        tyDeployInfoService.download(Convert.toList(TyDeployInfoDto.class, tyDeployInfoService.queryAll(criteria)), response);
    }

    @GetMapping
    @Log("查询团油配置")
    @ApiOperation("查询团油配置")
    @PreAuthorize("@el.check('admin','tyDeployInfo:list')")
    public ResponseEntity<Object> getTyDeployInfos(TyDeployInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(tyDeployInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增团油配置")
    @ApiOperation("新增团油配置")
    @PreAuthorize("@el.check('admin','tyDeployInfo:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody TyDeployInfo resources){
        return new ResponseEntity<>(tyDeployInfoService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改团油配置")
    @ApiOperation("修改团油配置")
    @PreAuthorize("@el.check('admin','tyDeployInfo:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody TyDeployInfo resources){
        tyDeployInfoService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除团油配置")
    @ApiOperation("删除团油配置")
    @PreAuthorize("@el.check('admin','tyDeployInfo:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody List<Integer> ids) {
        tyDeployInfoService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}